/**
 * Created by dqw on 2015/12/30.
 */

//列表开始
var dtGridColumns = [
    {
        id: 'delFlag',
        type: 'int',
        hideQuery:true,
        'export':false,
        hideQueryType:'eq',
        hideQueryValue:1,
        hide:true
    },
    {
        id: 'status',
        type: 'int',
        hideQuery:true,
        'export':false,
        hideQueryType:'eq',
        hideQueryValue:3,
        hide:true
    },
    {
        id: 'id',
        title: 'ID',
        type: 'number',
        columnClass: 'text-center width-20',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'eq',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            return '<a href="' + ncGlobal.adminRoot + 'orderHold/look?id=' + record.id + '">' + value + '</a>';
        }
    },
    {
        id: 'userId',
        title: '客户ID',
        type: 'number',
        headerClass: 'text-left',
        columnClass: 'text-left width-20',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'eq',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            return '<a href="' + ncGlobal.adminRoot + 'user/look?id=' + record.userId + '">' + value + '</a>';
        }
    },
    {
        id: 'inviterId',
        title: '上级ID',
        type: 'number',
        headerClass: 'text-left',
        columnClass: 'text-left width-20',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'eq',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            return '<a href="' + ncGlobal.adminRoot + 'user/look?id=' + record.inviterId + '">' + value + '</a>';
        }
    },
    {
        id: 'u.userName',
        title: '客户姓名',
        type: 'string',
        headerClass: 'text-left',
        columnClass: 'text-left width-20',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            return '<a href="' + ncGlobal.adminRoot + 'orderHold/look?id=' + record.id + '">' + value + '</a>';
        }
    },
    // {
    //     id: 'phone',
    //     title: '电话',
    //     type: 'string',
    //     headerClass: 'text-left',
    //     columnClass: 'text-left',
    //     fastSort: false
    // },
    {
        id: 'equityCode',
        title: '股票代码',
        type: 'string',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },

    {
        id: 'equityName',
        title: '股票名称',
        type: 'string',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'tradeCredit',
        title: '点买额度(万)',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-right',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'range',
         format: '#,##0'
    },
    // {
    //     id: 'buyPrice',
    //     title: '点买价格',
    //     type: 'number',
    //     headerClass: 'text-center',
    //     columnClass: 'text-right',
    //     fastSort: false,
    //     fastQuery: true,
    //     fastQueryType: 'range',
    //     format: '#,##0.00'
    // },
    {
        id: 'buyNumber',
        title: '点买数量',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-right',
        fastSort: true,
        format: '#,##0'
    },
    {
        id: 'buyDealPrice',
        title: '买入成交价格',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-right',
        fastSort: true,
        format: '#,##0.000'
    },
    {
        id: 'buyDealNumber',
        title: '买入成交数量',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-right',
        fastSort: true,
        format: '#,##0'
    },
    {
        id: 'currentProfit',
        title: '浮动盈亏',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-right',
        fastSort: true,
        format: '#,##0',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            if (value > 0) {
                return '<span style="padding:2px 5px;color:#ff2a21;">' + value + '</span>';
            } else {
                return '<span style="padding:2px 5px;color:green;">' + value + '</span>';
            }
        }
    },
    {
        id: 'profitStopPrice',
        title: '止盈价格',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-right',
        fastSort: true,
        fastQuery: false,
        format: '#,##0.00'
    },
    {
        id: 'loseStopPrice',
        title: '止损价格',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-right',
        fastSort: true,
        fastQuery: false,
        format: '#,##0.00'
    },
    {
        id: 'i.userName',
        title: '投资人姓名',
        type: 'string',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'buyTime',
        title: '点买时间',
        type: 'date',
        format: 'yyyy-MM-dd',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'range'
    },
    {
        id: 'buyDealTime',
        title: '点买成交时间',
        type: 'date',
        format: 'yyyy-MM-dd',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'range'
    },
    {
        id: 'acceptTime',
        title: '投资人接单时间',
        type: 'date',
        format: 'yyyy-MM-dd',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'range'
    },
    {
        id: 'status',
        title: '订单状态',
        type: 'string',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false,
        hideQuery:true,
        hideQueryType:'eq',
        hideQueryValue:3,
        codeTable:{
            3:"持仓中"
        },
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            if (record.status == 1) {
                return '<span style="background:#008000;padding:2px 5px;color:white;">匹配中</span>';
            }
            if (record.status == 2) {
                return '<span style="background:#568058;padding:2px 5px;color:white;">交易中</span>';
            }
            if (record.status == 3) {
                return '<span style="background:#0000FF;padding:2px 5px;color:white;">持仓中</span>';
            }
            if (record.status == 4) {
                return '<span style="background:#FFDF9B;padding:2px 5px;color:white;">待平仓</span>';
            }
            if (record.status == 5) {
                return '<span style="background:#008000;padding:2px 5px;color:white;">平仓中</span>';
            }
            if (record.status == 6) {
                return '<span style="background:#FFDF9B;padding:2px 5px;color:white;">待结算</span>';
            }
            if (record.status == 7) {
                return '<span style="background:black;padding:2px 5px;color:white;">已结算</span>';
            }
            if (record.status == 8) {
                return '<span style="background:#747372;padding:2px 5px;color:white;">流单</span>';
            }
        }
    },
    {
        id: 'look',
        title: '操作',
        type: 'string',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false,
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            // var look = "<a href='" + ncGlobal.adminRoot + "orderHold/look?id=" + record.id + "' class='btn btn-xs btn-primary m-r-10' data-toggle='modal'><i class='fa fa-view'></i>查看</a>";
            var sell = "<a data-target='#addModal' class='btn btn-xs btn-primary m-r-10' data-toggle='modal' data-no='" + dataNo + "'><i class='fa fa-edit'></i>平仓</a>";
            var modify = "<a data-target='#modifyModal' class='btn btn-xs btn-primary m-r-10' data-toggle='modal' data-no='" + dataNo + "'><i class='fa fa-edit'></i>修改</a>";
            var repairLoss = "<a data-target='#modifyModal2' class='btn btn-xs btn-primary m-r-10' data-toggle='modal' data-no='" + dataNo + "'><i class='fa fa-edit'></i>补亏</a>";

            var html = "";
            if ($("#pingPermi") && $("#pingPermi").val() == 1) {
                html += sell;
            }
            if ($("#modifyPermi") && $("#modifyPermi").val() == 1) {
                html += modify;
            }
            if ($("#repairPermi") && $("#repairPermi").val() == 1) {
                html += repairLoss;
            }
            return html;
        }
    }
    // {
    //     id: 'look',
    //     title: '查看',
    //     type: 'string',
    //     headerClass: 'text-left',
    //     columnClass: 'text-left',
    //     fastSort: false,
    //     resolution: function (value, record, column, grid, dataNo, columnNo) {
    //         var look = "<a href='" + ncGlobal.adminRoot + "orderHold/look?id=" + record.id + "' class='btn btn-xs btn-primary m-r-10' data-toggle='modal'><i class='fa fa-edit'></i>&nbsp;查看&nbsp;</a>";
    //         var html = "";
    //         // 判断是否有编辑权限
    //         if ($("#lookPermi") && $("#lookPermi").val() == 1) {
    //             html += look;
    //         }
    //         return html;
    //     }
    // }
];

var dtGridOption = {
    lang: 'zh-cn',
    ajaxLoad: true,
    loadURL: ncGlobal.adminRoot + 'order/list.json',
    exportURL:ncGlobal.adminRoot + 'order/listExport.json',
    exportFileName: '持仓中列表',
    columns: dtGridColumns,
    gridContainer: 'dtGridContainer',
    toolbarContainer: 'dtGridToolBarContainer',
    pageSize: 20,
    pageSizeLimit: [20, 30, 50],
    ncColumnsType: {
        int: ["id", "userId", "limit", "investId", "tradeCredit", "status","inviterId"],
        Timestamp: ["buyTime", "acceptTime", "buyDealTime", "eveningUpTime", "sellTime", "sellDealTime", "settleTime"],
        BigDecimal: ["buyPrice", "sellPrice", "profitCount","profitStopPrice","loseStopPrice"]
    },
    tools:'refresh|faseQuery|export[excel]',
};

var grid = $.fn.DtGrid.init(dtGridOption);
//默认查询条件
grid.fastQueryParameters = new Object();
grid.fastQueryParameters['eq_delFlag'] = 1;
if ($('#roleId').val()== 3){
    //销售
    // grid.fastQueryParameters['eq_inviter_id'] = $('#userId').val();
}
if ($('#roleId').val()== 2){
    grid.fastQueryParameters['eq_investor_id'] = $('#userId').val();
}
if ($('#roleId').val()== 0){
}
grid.fastQueryParameters['eq_status'] = 3;

//排序
grid.sortParameter.columnId = ['desc_acceptTime','desc_buyDealTime'];
grid.sortParameter.sortType = 1;
setInterval(function () {
    grid.refresh(true)
}, 2000);

//列表结束
//操作处理开始
var OperateHandle = function () {

    function _bindEvent() {

        //新增对话框初始化
        $("#addModal").on("show.bs.modal", function (event) {
            //获取接受事件的元素
            var button = $(event.relatedTarget);
            //获取data 参数
            var datano = button.data('no');
            var modal = $(this);
            //获取列表框中的原始数据
            var gridData = grid.sortOriginalDatas[datano];
            var id = gridData.id;
            $(".oId").val(gridData.id);
            //清除错误信息
            $(".alert-danger").remove();
            $("#addForm").psly().reset();
        });

        //修改对话框初始化
        $("#modifyModal").on("show.bs.modal", function (event) {
            //清除错误信息
            $(".alert-danger").remove();
            $("#modifyForm").psly().reset();
            //获取接受事件的元素
            var button = $(event.relatedTarget);
            //获取data 参数
            var datano = button.data('no');
            var modal = $(this);
            //获取列表框中的原始数据
            var gridData = grid.sortOriginalDatas[datano];

            $("#modifyOrderId").val(gridData.id);
        });
        //补亏对话框初始化
        $("#modifyModal2").on("show.bs.modal", function (event) {
            //清除错误信息
            $(".alert-danger").remove();
            $("#modifyForm2").psly().reset();
            //获取接受事件的元素
            var button = $(event.relatedTarget);
            //获取data 参数
            var datano = button.data('no');
            var modal = $(this);
            //获取列表框中的原始数据
            var gridData = grid.sortOriginalDatas[datano];

            $("#orderId").val(gridData.id);
            $("#repair").val('');

        });

        //编辑对话框初始化
        $("#editModal").on("show.bs.modal", function (event) {
            //清除错误信息
            $(".alert-danger").remove();
            $("#editForm").psly().reset();
            //获取接受事件的元素
            var button = $(event.relatedTarget);
            //获取data 参数
            var datano = button.data('no');
            var modal = $(this);
            //获取列表框中的原始数据
            var gridData = grid.sortOriginalDatas[datano];

            $("#adminId").val(gridData.id);
            $("#adminName").val(gridData.name);
            modal.find("[name='password']").val("");
            modal.find("[name='password2']").val("");
            modal.find("[name='roleId']").val(gridData.roleId);
        });

        //模糊搜索
        $('#customSearch').click(function () {
            grid.fastQueryParameters = new Object();
            grid.fastQueryParameters['eq_delFlag'] = 1;
            grid.fastQueryParameters['eq_status'] = 3;
            grid.fastQueryParameters['lk_name'] = $('#keyword').val();
            grid.pager.startRecord = 0;
            grid.pager.nowPage = 1;
            grid.pager.recordCount = -1;
            grid.pager.pageCount = -1;
            grid.refresh(true);
        });
    }

    //外部可调用
    return {
        bindEvent: _bindEvent
    }
}();
//操作处理结束

$(function () {
    //加载列表
    grid.load();
    //页面绑定事件
    OperateHandle.bindEvent();
});